package com.example.safeassistan.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.cy.tablayoutniubility.FragPageAdapterVpNoScroll;
import com.cy.tablayoutniubility.TabAdapterNoScroll;
import com.cy.tablayoutniubility.TabLayoutNoScroll;
import com.cy.tablayoutniubility.TabMediatorVpNoScroll;
import com.cy.tablayoutniubility.TabNoScrollViewHolder;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.message.fragment.MessageFragment;
import com.example.safeassistan.message.fragment.MessageSendFragment;

import java.util.ArrayList;
import java.util.List;
/**
 * 消息列表
 * */
public class Activity_Message extends BaseActivity implements View.OnClickListener {
    private TabLayoutNoScroll top_bar;
    private ViewPager viewpager;
    private ImageView _barIvRight;

    public static void actionStart(Context context ) {
        Intent intent = new Intent(context, Activity_Message.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {
        setTitleText("消息");
        _barIvRight=findViewById(R.id._barIvRight);
        _barIvRight.setVisibility(View.VISIBLE);
        _barIvRight.setOnClickListener(this);
        _barIvRight.setImageResource(R.mipmap.icon_msg_send);
        top_bar=findViewById(R.id.top_bar);
        viewpager=findViewById(R.id.viewpager);

        FragPageAdapterVpNoScroll<String> fragmentPageAdapter = new FragPageAdapterVpNoScroll<String>(getSupportFragmentManager(),7) {

            @Override
            public Fragment createFragment(String bean, int position) {
                switch (position){
                    case 0:
                        return MessageFragment.newInstance(1);
                    case 1:
                        return MessageSendFragment.newInstance(2);
                }
                return  null;

            }

            @Override
            public void bindDataToTab(TabNoScrollViewHolder holder, int position, String bean, boolean isSelected) {
                TextView textView = holder.getView(R.id.tv);
                if (isSelected) {
                    textView.setTextColor(getResources().getColor(R.color.gray3));
                    textView.setTextSize(14);
                } else {
                    textView.setTextColor(getResources().getColor(R.color.gray9));
                    textView.setTextSize(14);
                }
                textView.setText(bean);
            }


            @Override
            public int getTabLayoutID(int position, String bean) {
                return R.layout.tab_item;
            }
        };
        TabAdapterNoScroll<String> tabAdapter = new TabMediatorVpNoScroll<String>(top_bar, viewpager).setAdapter(fragmentPageAdapter);

        List<String> list = new ArrayList<>();
        list.add("收件箱");
        list.add("发件箱");
        fragmentPageAdapter.add(list);
        tabAdapter.add(list);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_RIGHT_IMG;
    }

    private PopupWindow popupWindow=null;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id._barIvRight:
                //发送消息
                View inflate = LayoutInflater.from(getContext()).inflate(R.layout.popu_top_msg,null);
                LinearLayout llMessage=inflate.findViewById(R.id.llMessage);
                LinearLayout llNotice=inflate.findViewById(R.id.llNotice);
                llNotice.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //发通知
                        SendMessageNotice.actionStart(getContext(),1);
                    }
                });
                llMessage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //发消息
                        SendMessageNotice.actionStart(getContext(),0);
                    }
                });
                popupWindow = new PopupWindow(inflate, RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                //设置背景,这个没什么效果，不添加会报错
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                //设置点击弹窗外隐藏自身
                popupWindow.setFocusable(true);
                popupWindow.setOutsideTouchable(true);
                //相对于某一个控件的位置
                popupWindow.showAsDropDown(_barIvRight,0,0);
                break;
        }
    }
}
